/*
 * Copyright 2010 dev7.jp
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.dev7.android.util;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Activities {

    private Activities() {
    }

    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(Activity activity, int id) {
        return (T) activity.findViewById(id);
    }

    public static void setClickListnerToButton(Activity activity, int id,
            OnClickListener onClickListener) {
        final Button button = findViewById(activity, id);
        if (button != null) {
            button.setOnClickListener(onClickListener);
        }
    }

    public static void setText(Activity activity, int id, CharSequence text) {
        final TextView v = findViewById(activity, id);
        if (v != null) {
            v.setText(text);
        }
    }

    public static Cursor getFromProvider(Activity activity, String uri) {
        return getFromProvider(activity, Uri.parse(uri));
    }

    public static Cursor getFromProvider(Activity activity, Uri uri) {
        return activity.managedQuery(uri, null, null, null, null);
    }

    public static void finishApplication(Activity activity) {
        activity.moveTaskToBack(true);
    }

}
